<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<style>
    .fs-label {
        color: #333 !important;
    }
</style>
<form:form commandName="bnpUser" cssClass="form-horizontal" role="form">
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left;" for="name_edit">用户姓名</label>
        <div class="col-sm-4">
            <input type="text" name="name" id="name_edit" value="${bnpUser.name}"
                   class="form-control" readonly/>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left;" for="phone_edit">用户手机号</label>
        <div class="col-sm-4">
            <input type="text" name="phone" id="phone_edit" value="${bnpUser.phone}"
                   class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left;" for="branch_edit">所属单位</label>
        <div class="col-sm-4">
            <select name="branch" id="branch_edit" class="form-control" required>
                <option value="">请选择</option>
                <c:forEach items="${branchs}" var="branchs">
                    <option value="${branchs}">${branchs}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left;"
               for="branchType_edit">单位类型</label>
        <div class="col-sm-4">
            <select name="branchType" id="branchType_edit" class="form-control" required>
                <option value="">请选择</option>
                <option value="0">业主单位</option>
                <option value="1">审批部门</option>
                <option value="2">管理员</option>
                <option value="4">督查组</option>
                <option value="6">分管领导</option>
                <option value="7">主要领导</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left;" for="role_edit">用户角色</label>
        <div class="col-sm-4">
                <%--<input type="text" name="role" id="role_edit" value=""--%>
                <%--class="form-control ">--%>
            <select name="role" id="role_edit" class="form-control" required>
                <option value="">请选择</option>
                <option value="经办人">经办人</option>
                <option value="责任人">责任人</option>
                <option value="领导">领导</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left; color: red;" for="deptId_edit">所属单位（正式）</label>
        <div class="col-sm-4 openSelect" >
            <select name="deptId" id="deptId_edit" class="form-control required departments " required>
                <optgroup label="">
                    <c:forEach items="${departments}" var="departments">
                        <option value="${departments.sysId}">${departments.deptName}</option>
                    </c:forEach>
                </optgroup>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left; color: red;"
               for="roleId_select">用户角色（正式）</label>
        <div class="col-sm-4">
            <select name="roleId" id="roleId_select" class="form-control" required>
                <option value="">请选择</option>
                <c:forEach items="${roles}" var="roles">
                    <option value="${roles.sysId}">${roles.roleName}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3"></div>
        <label class="col-sm-2 control-label no-padding-right" style="text-align: left; color: red;"
               for="effectFlag_select">是否生效（正式）</label>
        <div class="col-sm-4">
            <select name="effectFlag" id="effectFlag_select" class="form-control" required>
                <option value="">请选择</option>
                <option value="1">是</option>
                <option value="0">否</option>
            </select>
        </div>
    </div>
    <input type="text" name="sysid" value="${bnpUser.sysId}" hidden/>
</form:form>
<script>
    $(function () {
        //可修改标志
        var editFlag = true;

        jQuery(function ($) {
            $(".date-picker").datepicker({
                inline: true,
                format: "yyyymmdd",
                autoclose: true,
                todayHighlight: true,
                language: 'cn'
            }).next().on(ace.click_event, function () {
                $(this).prev().focus();
            });
        });

        //所属单位
        var branch = '${bnpUser.branch}';
        var branchs = $("#branch_edit>option");
        for (var i = 0; i < branchs.length; i++) {
            if (branchs[i].value == branch) {
                branchs[i].setAttribute("selected", "true");
            } else {
                branchs[i].removeAttribute("selected");
            }
        }


        //单位类型
        var branchType = '${bnpUser.branchType}';
        var branchTypes = $("#branchType_edit>option");
        for (var i = 0; i < branchTypes.length; i++) {
            if (branchTypes[i].value == branchType) {
                branchTypes[i].setAttribute("selected", "true");
            } else {
                branchTypes[i].removeAttribute("selected");
            }
        }


        //用户角色
        var role = '${bnpUser.role}';
        var roles = $("#role_edit>option");
        for (var i = 0; i < roles.length; i++) {
            if (roles[i].value == role) {
                roles[i].setAttribute("selected", "true");
            } else {
                roles[i].removeAttribute("selected");
            }
        }

        $("#phone_edit").blur(function () {
            var oldPhone = '${bnpUser.phone}';
            var name = $("#name_edit").val();
            var phone = $("#phone_edit").val();
            $.ajax({
                type: "GET",
                url: "/zhApp/queryByPhone",
                dataType: "json",
                data: {phone: phone},
                success: function (model) {
                    var list = model.data.list;
                    if (list.length == 1) {
                        var n = list[0].name;
                        if (name != n) {
                            alert("手机号码验证不通过！");
                            $("#phone_edit").val(oldPhone);
                            editFlag = false;
                        }
                    } else if (list.length == 0) {
                        editFlag = true;
                    } else {
                        alert("手机号码验证不通过！");
                        $("#phone_edit").val(oldPhone);
                        editFlag = false;
                    }
                },
                error: function (model) {
                }
            });
        });

        //点击修改用户
        function editUser() {
            var phone = $("#phone_edit").val();
            var name = $("#name_edit").val();
            $.ajax({
                type: "GET",
                url: "/zhApp/queryByPhone",
                dataType: "json",
                async: false,
                data: {phone: phone},
                success: function (model) {
                    var list = model.data.list;
                    if (list.length == 1) {
                        var n = list[0].name;
                        if (name != n) {
                            editFlag = false;
                        }
                    } else if (list.length == 0) {
                        editFlag = true;
                    } else {
                        editFlag = false;
                    }
                },
                error: function (model) {
                }
            });
            if (!editFlag) {
                alert("手机号码验证不通过！");
                return false;
            }
            var win = confirm("确认修改此用户信息？");
            if (win == true) {
                executeSaveOrUpdate({
                    formId: "#formDialog form",
                    dialogId: "#formDialog",
                    url: getUrl("editUser"),
                    tableId: "#gridTable",
                    successMessage: "修改成功",
                    successTitle: '操作对话框',
                    //failMessage: '更新失败',
                    failTitle: '消息对话框'
                });
                //倒计时
                var btn = document.getElementById("submit");
                btn.disabled = true;
                countdown(5);
            }
        }

        var effectFlag = '${bnpUser.effectFlag}';
        $("#effectFlag_select").val(effectFlag);
        var roleId = '${role.sysId}';
        $("#roleId_select").val(roleId);

    });

    //单选搜索
    $('.departments').fSelect();
    $('.departments').change(function () {
        $(".fs-dropdown").css("display", "block");
    });
    var deptName = '${userDept.deptName}';
    console.log(typeof (deptName));
    if(deptName == ""){
        $(".fs-label").text("请选择");
    }else {
        $(".fs-label").text(deptName);
    }
</script>